<template>
  <!-- 登录 -->
  <view class="content box">
    <view class="box_images">
      <image src="../../static/images/bg_dengluzhuce.png" mode=""></image>
    </view>
    <view class="box_title">
      <view class="boxt">新用户注册</view>
      <view class="boxc">欢迎注册华胥星球，开始你的探索之旅吧！</view>
    </view>

    <view class="box_inp">
      <u-tabs
        :list="list"
        @click="tabChange"
        lineColor="#55b877"
        :activeStyle="{ color: '#92959d' }"
      ></u-tabs>
      <!-- 手机号码输入 -->
      <u-input
        class="input"
        :placeholder="selectItem == 0 ? '请输入手机号码' : '请输入邮箱'"
        border="none"
        shape="circle"
        color="#888888"
        :fontSize="inputFont"
        v-model="mobile"
        @change="change"
        placeholderStyle="color:#888888;padding-left:50rpx"
      ></u-input>
      <!-- <view class="verify" v-show="mobile == ''">请输入手机号</view> -->

      <!-- 验证码输入 -->
      <u-input
        class="inputPassword"
        type="number"
        placeholder="请输入验证码"
        maxlength="6"
        border="none"
        shape="circle"
        color="#888888"
        :fontSize="inputFont"
        v-model="smsCode"
        @change="change"
        placeholderStyle="color:#888888;padding-left:50rpx"
      >
        <template slot="suffix">
          <u-code
            ref="uCode"
            @change="codeChange"
            seconds="120"
            changeText="Xs"
          ></u-code>
          <view @tap="getCode" class="codeTip">{{ tips }}</view>
        </template>
      </u-input>
      <!-- <view class="verify" v-show="smsCode == ''">请输入验证码</view> -->

      <!-- 密码输入 -->
      <u-input
        class="inputPassword"
        type="password"
        placeholder="请输入6~12位字母、数字混合密码"
        maxlength="20"
        border="none"
        shape="circle"
        color="#888888"
        :fontSize="inputFont"
        v-model="password"
        @change="change"
        placeholderStyle="color:#888888;padding-left:50rpx"
      ></u-input>
      <!-- <view class="verify" v-show="password == ''"
        >密码格式不正确请输入6~12位字母、数字混合密码</view
      > -->
      <!-- 密码再次确认 -->
      <u-input
        class="inputPassword"
        type="password"
        placeholder="请再次确认登录密码"
        maxlength="20"
        border="none"
        shape="circle"
        color="#888888"
        :fontSize="inputFont"
        v-model="password_two"
        @change="change"
        placeholderStyle="color:#888888;padding-left:50rpx"
      ></u-input>
      <!-- <view class="verify" v-show="password_two == ''">与设置密码不一致</view> -->

      <!-- 邀请码（选填） -->
      <u-input
        class="inputPassword"
        type="password"
        placeholder="请输入邀请码（选填）"
        maxlength="20"
        border="none"
        shape="circle"
        color="#888888"
        :fontSize="inputFont"
        v-model="invitation_code"
        @change="change"
        placeholderStyle="color:#888888;padding-left:50rpx"
      ></u-input>
      <!-- <view class="verify" v-show="invitation_code == ''">邀请码无效</view> -->

      <view class="login">
        <view class="loginFont" @click="register">注册</view>
      </view>

      <view class="account">
        <span class="noAccount">已有账号，</span>
        <span class="registerAccount" @click="login">立即登录</span>
      </view>
    </view>
    <!-- 用户服务协议 隐私政策》 -->
    <view class="box_bottom">
      <!-- <view>
        <u-checkbox-group v-model="checked" placement="row">
          <u-checkbox
            shape="circle"
            activeColor="#55B877"
            inactiveColor="#888888"
            iconSize="12"
          ></u-checkbox>
        </u-checkbox-group>
      </view> -->
      <view @click="selects">
        <view v-if="selected" class="select">
          <image src="../../static/images/xuanzhong.png" mode=""></image>
        </view>
        <view v-else class="select">
          <image src="../../static/images/weixuanzhong.png" mode=""></image>
        </view>
      </view>
      <view>我已阅读并同意</view>
      <view class="user">《用户服务协议》</view>
      <view>和</view>
      <view class="user">《隐私政策》</view>
    </view>
  </view>
</template>

<script>
import storage from "@/common/storage.js"; // 缓存封装
export default {
  data() {
    return {
      mobile: "",
      smsCode: "",
      password: "",
      password_two: "",
      invitation_code: "",
      inputFont: "30rpx",
      isPassword: false,
      tips: "",
      checked: [],
      device: "",
      platform: "app",
      selected: false,
      email: "",
      list: [
        {
          name: "手机号注册",
        },
        {
          name: "邮箱注册",
        },
      ],
      selectItem: 0,
    };
  },
  created() {
    let that = this;
    // storage.set("token1", 666);
    // console.log(storage.get("token1"))
    // console.log(uni.getSystemInfo())
    // 获取设备信息
    uni.getSystemInfo({
      success: function (res) {
        // console.log( res)
        that.device = res.deviceModel;
      },
    });
  },
  methods: {
    // 切换注册方式
    tabChange(item) {
      // console.log('item', item);
      this.selectItem = item.index;
      this.mobile = ""
      this.smsCode = ""
      this.password = ""
      this.password_two = ""
      this.invitation_code = ""
    },
    selects() {
      this.selected = !this.selected;
    },
    change() {
      console.log(this.value);
    },
    codeChange(text) {
      this.tips = text;
    },
    getCode() {
      if (this.mobile == "") {
        if (this.selectItem == 0) {
          uni.$u.toast("请先输入手机号");
          return;
        } else {
          uni.$u.toast("请先输入邮箱");
          return;
        }
      }
      if (this.$refs.uCode.canGetCode) {
        if (this.selectItem == 0) {
          this.$http
            .sendSmsCaptcha({ mobile: this.mobile })
            .then((res) => {
              // 模拟向后端请求验证码
              uni.showLoading({
                title: "正在获取验证码",
              });
              setTimeout(() => {
                uni.hideLoading();
                // 这里此提示会被this.start()方法中的提示覆盖
                uni.$u.toast("验证码已发送");
                // 通知验证码组件内部开始倒计时
                this.$refs.uCode.start();
              }, 2000);
            })
            .catch((error) => {
              console.log(error);
              uni.$u.toast(error);
            });
        } else {
          this.$http
            .send_email({ email: this.mobile })
            .then((res) => {
              // 模拟向后端请求验证码
              uni.showLoading({
                title: "正在获取验证码",
              });
              setTimeout(() => {
                uni.hideLoading();
                // 这里此提示会被this.start()方法中的提示覆盖
                uni.$u.toast("验证码已发送");
                // 通知验证码组件内部开始倒计时
                this.$refs.uCode.start();
              }, 2000);
            })
            .catch((error) => {
              console.log(error);
              uni.$u.toast(error);
            });
        }
      } else {
        uni.$u.toast("倒计时结束后再发送");
      }
    },
    // 注册
    register() {
      if (this.mobile == "") {
        return uni.$u.toast("请输入手机号");
      }
      if (this.smsCode == "") {
        return uni.$u.toast("请输入验证码");
      }
      if (this.password == "") {
        return uni.$u.toast("请输入密码");
      }
      if (this.password_two == "") {
        return uni.$u.toast("请再次输入密码");
      }
      // if(this.invitation_code == ''){
      //   return uni.$u.toast("请输入邀请码");
      // }
      console.log(this.checked.length, 111111111111);
      if (!this.selected) {
        return uni.$u.toast("请勾选阅读用户服务协议和隐私政策");
      }
      this.$http
        .register({
          mobile: this.mobile,
          password: this.password,
          password_two: this.password_two,
          smsCode: this.smsCode,
          invitation_code: this.invitation_code,
          device: this.device,
          platform: this.platform,
          email_or_phone:this.selectItem == 0?1:2
        })
        .then((res) => {
          storage.set("token", res.data.token);
          uni.$u.toast("注册成功");
          uni.redirectTo({
            url: "/pages/login/index",
          });
        })
        .catch((error) => {
          console.log(error, 666666);
          uni.$u.toast(error);
        });
    },
    login() {
      uni.navigateTo({
        url: "index",
      });
    },
  },
};
</script>

<style lang="scss" scoped>
.box {
  background: #111111;
  padding: 212rpx 60rpx 0;
  box-sizing: border-box;
  // overflow-y: scroll;
}
.box_images {
  width: 370rpx;
  height: 266rpx;
  position: fixed;
  right: 0;
  top: 0;
  image {
    width: 100%;
    height: 100%;
  }
}
.box_title {
  .boxt {
    font-size: 56rpx;
    font-family: PingFang SC-Bold, PingFang SC;
    font-weight: bold;
    color: #fff;
  }
  .boxc {
    font-size: 30rpx;
    font-family: PingFang SC-Medium, PingFang SC;
    font-weight: bold;
    color: #a2a2a2;
    margin-top: 12rpx;
  }
}
.box_inp {
  width: 100%;

  .input {
    background-color: #272c29;
    width: 630rpx;
    height: 96rpx;
    margin-top: 20rpx;
  }
  .inputPassword {
    width: 630rpx;
    height: 96rpx;
    background-color: #272c29;
    margin-top: 40rpx;
    position: relative;

    .codeTip {
      position: absolute;
      right: 50rpx;
      bottom: 26rpx;
      font-size: 30rpx;
      font-weight: 400;
      color: #55b877;
    }
  }
  /deep/.uni-input-input {
    // left: 50rpx;
    padding-left: 50rpx;
  }
  .verify {
    margin: 16rpx 0 0 30rpx;
    font-size: 24rpx;
    font-family: PingFang SC-Medium, PingFang SC;
    font-weight: 400;
    color: #ee6060;
  }

  .inputBottom {
    margin-top: 32rpx;
    display: flex;
    justify-content: space-between;
    padding: 0 30rpx;

    .forgotPassw {
      font-size: 28rpx;
      font-weight: 400;
      color: #adadad;
    }
    .verCodeLogin {
      font-size: 28rpx;
      font-weight: 400;
      color: #55b877;
    }
  }

  .login {
    width: 630rpx;
    height: 96rpx;
    margin-top: 54rpx;
    background: linear-gradient(360deg, #50b674 0%, #aeddbd 100%);
    border-radius: 48rpx 48rpx 48rpx 48rpx;

    .loginFont {
      line-height: 96rpx;
      text-align: center;
      font-size: 30rpx;
      font-weight: 400;
      color: #ffffff;
    }
  }

  .account {
    text-align: center;
    margin-top: 32rpx;
    font-size: 28rpx;
    padding-bottom: 100rpx;

    .noAccount {
      color: #d8d8d8;
    }
    .registerAccount {
      color: #55b877;
    }
  }
}
.box_bottom {
  position: fixed;
  bottom: 40rpx;
  display: flex;
  font-size: 26rpx;
  color: #888888;
  .user {
    color: #55b877;
  }
  .select {
    width: 30rpx;
    height: 30rpx;
    margin: 2rpx 8rpx 0 0;
    image {
      width: 100%;
      height: 100%;
    }
  }
}

// text:first-child
</style>